package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	
	import game.view.GameStage;
	
	import morn.core.components.FrameClip;
	import morn.core.handlers.Handler;
	
	import tools.filterEffect.FlashFilterEffect;
	
	[SWF(width=840,height=620,frameRate=40,backgroundColor=0x000000)]
	//[SWF(width=640,height=480,frameRate=40,backgroundColor=0x000000)]
	//[SWF(frameRate=40,backgroundColor=0x000000)]
	public class main extends Sprite {
		
		private var myStage:GameStage;
		private var loadingSprite:Sprite;
		private var loadingFC:FrameClip;
		private var loadingTF:TextField;
		private var status:Boolean = false;
		
		private var set3366Status:Boolean = false;
		public static var open3366Service:* =null;
		
		private var set4399Status:Boolean = false;   // 是否有赋值过4399
		public static var _4399_function_score_id:String = "d8c8d4731a33a0a581edc746e73eadc7200";
		public static var _4399_function_gameList_id:String = "944c23f5e64a80647f8d0f3435f5c7a8";
		public static var serviceHold:* = null;
		
		
		//private var show4399Status:Boolean         = false;   // 是否启用4399
		private var show7k7kOrYouYouStatus:Boolean = false;  // 是否启用7k7kOrYouYou
		
		public function main() {
			this.addEventListener(Event.ENTER_FRAME, updateFrame);
		}
		
		public function setHold(hold:*):void {
			serviceHold = hold;
		}
		
		private function set4399Service():void {
			if(set4399Status) {
				return
			}
			if(serviceHold != null) {
				if(myStage != null) {
					set4399Status = true;
					myStage.set4399Service(serviceHold);
				}
			}
		}

		
		public function setService(service:*):void {  
			open3366Service = service;   
		}
		
		private function setOpen3366Service():void {
			if(set3366Status) {
				return;
			}
			
			if(open3366Service != null) {
				if(myStage != null) {
					set3366Status = true;
					myStage.set3366Service(open3366Service);
				}
			}
		}
		
		private function show7k7kOrYouYou():void {
			// 要先把一个文件丢进来
			// YouYou
			/*FWAd_AS3.showAd({
				container:this,
				x:0,
				y:0,
				wid:'840',
				hei:'620',
				id:'yilangdi-295712532557721377228519-1',
				adType:'loading',
				onClickStartBtn:loadingResources
			});*/
			
			// 7k7k
			/*FWAd_AS3.showAd({
				container:this,
				x:0,
				y:0,
				wid:this.stage.stageWidth,
				hei:this.stage.stageHeight,
				id:"yilangdi-113412529528321377228036-1-1",//此id为广告id，只在添加广告时使用，开发者请直接复制本段代码到程序中即可
				adType:"loading",
				onClickStartBtn:loadingResources
			});*/
		}
		
		private function stageOk():void {
			App.init(this);
			
			loadComplete();
			//App.loader.loadAssets(["bin/assets/vector.swf"], new Handler(loadComplete), new Handler(loadProgress));
			//App.loader.loadAssets(["bin/assets/comp.swf", "bin/assets/nav.swf", "bin/assets/other.swf", "bin/assets/xiaoguai.swf"], new Handler(loadComplete), new Handler(loadProgress));
		}
		
		private function loadComplete():void {
			if(show7k7kOrYouYouStatus == true) {
				show7k7kOrYouYou();
			}
			else {
				//addGame();
				loadingResources();
			}
		}
		
		private function loadProgress(value:Number):void {
			//加载进度
			//trace("loaded", value);
		}
		
		private function loadingResources():void {
			loadingSprite = new Sprite();
			this.addChild(loadingSprite);
			
			loadingFC          = new FrameClip();
			loadingFC.skin     = "assets.frameclip_load";
			loadingFC.autoPlay = true;
			loadingSprite.addChild(loadingFC);
			
			loadingTF = new TextField
			loadingTF.defaultTextFormat = FlashFilterEffect.myTextFormat2;
			loadingTF.width = 160;
			loadingSprite.addChild(loadingTF);
			
			loadingAutoSize();
			
			loadingTFShow();
			
			App.loader.loadAssets(["bin/assets/comp.swf", "bin/assets/nav.swf", "bin/assets/other.swf", "bin/assets/xiaoguai.swf"], new Handler(loadResourcesComplete), new Handler(loadResourcesProgress));
		}
		
		public function loadingTFShow():void {
			if(!loadingSprite.visible) {
				loadingSprite.visible = true;
				//this.setChildIndex(loadingSprite, this.numChildren - 1);
			}
		}
		
		public function loadingHide():void {
			loadingSprite.visible = false;
		}
		
		private function loadingAutoSize():void {
			if(loadingSprite != null) {
				loadingSprite.x = this.stage.stageWidth *.5;
				loadingSprite.y = this.stage.stageHeight *.5;
			}
		}
		
		private function loadResourcesProgress(value:Number):void {
			if(loadingTF != null) {
				var v:String = Math.floor(value * 100) + "%";
				
				loadingTF.x        = -(3 * 35)*.5;
				loadingTF.y        = -24;
				loadingTF.htmlText = v;
			}
		}
		
		private function loadResourcesComplete():void {
			loadingHide();
			addGame();
		}
		
		private function addGame():void {

			this.stage.addEventListener(Event.RESIZE, resizeWindow);
			
			//实例化场景
			myStage = new GameStage();
			addChild(myStage);
			myStage.init();
			
			resizeWindow();
		}
		
		private function updateFrame(e:Event):void {
			setOpen3366Service();
			
			set4399Service();
			
			if(status == true) {
				return;
			}
			
			if(this.hasOwnProperty("stage") && this.stage != null) {
				status = true;
				stageOk();
			}
		}
		
		private function resizeWindow(e:Event = null):void {
			//this.stage.stageWidth = 640;
			//this.stage.stageWidth = 640;
			if(myStage != null) {
				if(show7k7kOrYouYouStatus == true) {
					//myStage.sizeAuto3();
				}
				//else if(show4399Status) {
					//myStage.sizeAuto2();
				//}
				else {
					myStage.sizeAuto3();
				}
			}
		}
	}
}